package com.jt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.service.ItemService;
import com.jt.vo.SysResult;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemDescMapper itemDescMapper;

    /**
     * 分页SQL:
     * select * from tb_item limit 起始位置, 查询条数
     *
     * @param pageCurrent
     * @param pageSize
     * @return
     */
    @Override
    public EasyUITable findItemsByPage(Integer pageCurrent, Integer pageSize) {

//		  2. 手动进行分页
		/*
		long total = itemMapper.selectCount(null);
		int startIndex = (pageCurrent - 1) * pageSize;
		List<Item> records = itemMapper.findItemsByPage(pageCurrent, startIndex, pageSize);*/
//		  2. 使用分页对象   功能相当于PageHelper
        IPage page = new Page(pageCurrent, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("updated");
        page = itemMapper.selectPage(page, queryWrapper);
        long total = page.getTotal();                //  获取查询后的总记录数
        List<Item> records = page.getRecords();
        return new EasyUITable(total, records);
    }

    @Override
    @Transactional        //开启事务控制
    public int saveItem(Item item, ItemDesc itemDesc) {
//		  1. 业务层需要进行参数校验, 这里没有加, 以后注意
//		Date date = new Date();
//		item.setCreated(date).setUpdated(date);
//		  当数据插入时,  还需要设置物品的状态信息
        item.setStatus(1);
        /*在MP中,  如果完成主键自增, 则自动实现数据的回显, 回显会将所有没有赋值的数据全部更新到item上*/
        int rows = itemMapper.insert(item);
//		功能扩展, 添加商品详情
        itemDesc.setItemId(item.getId());
        int row = itemDescMapper.insert(itemDesc);
        return rows;
    }

    @Override
    public SysResult findById(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        List<Item> list = itemMapper.selectList(queryWrapper);
        return null;
    }

    @Override
    public int updateItem(Item item, ItemDesc itemDesc) {
        if (item == null) {
            throw new IllegalArgumentException("参数为空");
        }

//		  使用主键为条件进行修改时, 可以使用updateById(), 修改多值时使用updateById修改所有字段, 修改单值时用update
		/*QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("id", item.getId());
		int rows = itemMapper.update(item, queryWrapper);*/
        int rows = itemMapper.updateById(item);
        System.err.println(item);
//		  功能扩展, 更新商品详情
        itemDesc.setItemId(item.getId());

        itemDescMapper.updateById(itemDesc);

        return rows;
    }

    @Override
    @Transactional
    public int deleteByIds(Long[] ids) {
        System.out.println(Arrays.toString(ids));
        if (ids == null) {
            throw new IllegalArgumentException("参数为空");
        }

		/*	方法1: 手动遍历
		List<Long> list = new ArrayList<>();
		for (Long l :
				ids) {
			list.add(l);
		}*/
//		方法2: 调用Arrays.asList()方法

//		List<Long> list = Arrays.asList(ids);
        /*方法1:  使用mybatis-plus完成功能*/
//		int rows = itemMapper.deleteBatchIds(list);
//		方法2:   使用mybatis的方法手动写SQL语句实现删除功能
        itemDescMapper.deleteBatchIds(Arrays.asList(ids));
        int rows = itemMapper.deleteByIds(ids);
        return rows;
    }


    @Override
    @Transactional
    public int changeStatus(Long[] ids) {
        if (ids == null) {
            throw new IllegalArgumentException("参数为空");
        }
        /*方法1: 需要先进行查询, 在进行修改*/
        List<Item> list = itemMapper.selectBatchIds(Arrays.asList(ids));
        for (Item item :
                list) {
            if (item.getStatus() == 1) {
                item.setStatus(2);
            } else {
                item.setStatus(1);
            }
        }
//		itemMapper.update()
        list.forEach(item -> itemMapper.updateById(item));
        System.out.println(list);
        return list.size();
    }


    @Override
    @Transactional
    public int updateStatus(Integer status, Long[] ids) {
        /*方法2: 使用update操作, 只使用update进行修改*/
        System.out.println(status);
        Item item = new Item();
        item.setStatus(status);
//		QueryWrapper queryWrapper = new QueryWrapper();
//		queryWrapper.in("id", ids);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.in("id", ids);
        int rows = itemMapper.update(item, updateWrapper);
        return rows;
    }

    @Override
    public ItemDesc findItemDescById(Long id) {
        ItemDesc itemDesc = itemDescMapper.selectById(id);
        return itemDesc;
    }
}
